package cn.basic.social.api.dingtalk.channel.internal.request;

import cn.basic.social.api.CommonConstants;
import cn.basic.social.api.dingtalk.BaseDingRequest;
import cn.basic.social.api.dingtalk.channel.internal.response.InternalGetAccessTokenResponse;
import cn.basic.social.api.exception.ApiRuleException;
import cn.basic.social.api.utils.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName InternalGetAccessTokenRequest
 * @Description 获取企业内部应用的accessToken请求
 * https://open.dingtalk.com/document/orgapp/obtain-the-access_token-of-an-internal-app
 * @Author yk.zlf
 * @Date 2023/10/20 16:08
 */
public class InternalGetAccessTokenRequest extends BaseDingRequest<InternalGetAccessTokenResponse> implements Serializable {

    private final String requestUrl = "https://api.dingtalk.com/v1.0/oauth2/accessToken";
    private String topHttpMethod = CommonConstants.HTTP_METHOD_POST;
    private String topContentType = "application/json";

    /**
     * 已创建的企业内部应用的AppKey
     */
    private String appKey;

    /**
     * 已创建的企业内部应用的AppSecret
     */
    private String appSecret;

    public InternalGetAccessTokenRequest() {
    }

    public InternalGetAccessTokenRequest(String appKey, String appSecret) {
        this.appKey = appKey;
        this.appSecret = appSecret;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    @Override
    public String getTopHttpMethod() {
        return this.topHttpMethod;
    }

    @Override
    public void setTopHttpMethod(String topHttpMethod) {
        this.topHttpMethod = topHttpMethod;
    }

    @Override
    public Map<String, String> getTextParams() {
        Map<String, String> params = new HashMap<>();
        params.put("appKey", this.appKey);
        params.put("appSecret", this.appSecret);
        return params;
    }

    @Override
    public Class<InternalGetAccessTokenResponse> getResponseClass() {
        return InternalGetAccessTokenResponse.class;
    }

    @Override
    public void check() throws ApiRuleException {
        if (StringUtils.isBlank(this.appKey)) {
            throw new ApiRuleException("-1", "获取企业内部应用的accessToken失败，缺少必要参数：appKey");
        }
        if (StringUtils.isBlank(this.appSecret)) {
            throw new ApiRuleException("-1", "获取企业内部应用的accessToken失败，缺少必要参数：appSecret");
        }
    }

    @Override
    public String getTopContentType() {
        return this.topContentType;
    }

    @Override
    public void setTopContentType(String topContentType) {
        this.topContentType = topContentType;
    }

    @Override
    public String getRequestUrl() {
        return this.requestUrl;
    }
}
